define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'merchantuser/index' + location.search,
                    add_url: 'merchantuser/add',
                    edit_url: 'merchantuser/edit',
                    del_url: 'merchantuser/del',
                    multi_url: 'merchantuser/multi',
                    import_url: 'merchantuser/import',
                    table: 'merchantuser',
                }
            });

            var table = $("#table");
            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                //这里我们手动设置底部的值
                if(data.merchant_id>=0){
                    Table.api.init({
                        extend: {
                            index_url: 'merchantuser/index' + location.search,
                            add_url: 'merchantuser/add?merchant_id='+data.merchant_id,
                            edit_url: 'merchantuser/edit?merchant_id='+data.merchant_id,
                            del_url: 'merchantuser/del',
                            multi_url: 'merchantuser/multi',
                            import_url: 'merchantuser/import',
                            table: 'merchantuser',
                        }
                    });
                }
                $('label[for="merchant_id"]').parent().css('display','none');
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                visible: false,
                showToggle: false,
                showColumns: false,
                search: false,
                showExport: false,
                escape: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'merchant_id', title: __('Merchant_id'),visible: false},
                        {field: 'merchantpost.name', title: __('Merchantpost_id'), operate: 'LIKE'},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'avatar', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        /*{field: 'username', title: __('Username'), operate: 'LIKE'},
                        {field: 'password', title: __('Password'), operate: 'LIKE'},
                        {field: 'salt', title: __('Salt'), operate: 'LIKE'},*/
                        {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'merchant.name', title: __('Shop.name'), operate: 'LIKE'},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
            $("#c-merchantpost_id").data("params", function(){
                console.log($("#c-merchant_id").val());
                return {custom: {merchant_id: $("#c-merchant_id").val()}};
            });
        },
        edit: function () {
            Controller.api.bindevent();
            $("#c-merchantpost_id").data("params", function (obj) {
                return {custom: {merchant_id: $("#c-merchant_id").val()}};
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});